﻿#region 命名空间

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Linq;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.Window
{
    [Access]
    public partial class GetTemplate : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strDir;
                strDir = Server.MapPath("~/Templates/" + TemplatesManage.GetUseing.TemplatePath);

                var str = strDir;
                str = str.Replace(Server.MapPath("~/"), "/");
                str = str.Replace("\\", "/");
                this.DropDownList1.Items.Insert(0, new ListItem(str, strDir));
                BindList("");
            }
        }

        private void BindList(string strDir)
        {
            this.GV_List.EmptyDataTemplate = WebFactory.PageBase.GetTemplate(this);

            var dt_Files = new DataTable();

            DataRow dr_Files;


            dt_Files.Columns.Add(new DataColumn("ICO", Type.GetType("System.String")));
            dt_Files.Columns.Add(new DataColumn("Name", Type.GetType("System.String")));
            dt_Files.Columns.Add(new DataColumn("FullName", Type.GetType("System.String")));


            dt_Files.Columns.Add(new DataColumn("Get", Type.GetType("System.String")));


            //针对当前目录建立目录引用对象

            if (string.IsNullOrEmpty(strDir))
            {
                strDir = Server.MapPath("~/Templates/" + TemplatesManage.GetUseing.TemplatePath);
            }


            var dirInfo = new DirectoryInfo(strDir);
            //循环判断当前目录下的文件和目录
            FileInfo fi;
            DirectoryInfo di;

            foreach (var fsi in dirInfo.GetFileSystemInfos().OrderBy(x => x.Extension))
            {
                //如果是文件
                if (fsi is FileInfo)
                {
                    dr_Files = dt_Files.NewRow();
                    fi = fsi as FileInfo;

                    dr_Files["ICO"] = fi.Extension.Replace(".", "") + ".gif";

                    dr_Files["Name"] = fi.Name;
                    dr_Files["FullName"] = fi.FullName;

                    var str =
                        fi.FullName.Replace(Server.MapPath("~/Templates/" + TemplatesManage.GetUseing.TemplatePath), "");
                    str = str.Replace("\\", "/");

                    dr_Files["Get"] = "<input type=\"button\" class=\"btn\" value=\"选择\" onclick='ClickRetrun(\"" + str +
                                      "\");' />";

                    dt_Files.Rows.Add(dr_Files);
                }
                else
                {
                    dr_Files = dt_Files.NewRow();

                    di = fsi as DirectoryInfo;
                    dr_Files["ICO"] = "folder.gif";
                    dr_Files["Name"] = di.Name;
                    dr_Files["FullName"] = di.FullName;


                    dt_Files.Rows.Add(dr_Files);
                }
            }
            this.GV_List.DataSource = dt_Files;
            this.GV_List.DataBind();
        }


        protected string GetTempalteURL(object InPut)
        {
            var OutPut = "ClickRetrun(\"" + InPut.ToString().Replace("\\", "/") + "\")";
            return OutPut;
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "GOTO")
            {
                var strDir = e.CommandArgument.ToString();

                if (Directory.Exists(strDir))
                {
                    var str = strDir;
                    str = str.Replace(Server.MapPath("~/"), "/");
                    str = str.Replace("\\", "/");
                    var li = new ListItem(str, strDir);
                    if (this.DropDownList1.Items.Contains(li) == false)
                    {
                        this.DropDownList1.Items.Insert(this.DropDownList1.Items.Count - 1, li);
                        this.DropDownList1.SelectedValue = strDir;
                    }

                    BindList(strDir);
                }
                else
                {
                    strDir = strDir.Replace(Server.MapPath("~/"), "/");
                    strDir = strDir.Replace("\\", "/");
                    MyJavascript.RegJS(this.UpdatePanel1, "ClickRetrun(\"" + strDir + "\");");
                }
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindList(this.DropDownList1.SelectedValue);
        }
    }
}